package com.shouhuobao.samecity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppCache;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.SameCityHistoryBean;
import com.collectplus.express.model.SameCityParcelBean;

/**
 * Created by lidandan on 2017/12/27.
 * 同城寄件人历史记录
 */
public class SameCityAddressHistoryActivity extends BaseActivity2 {

    private ListView mListView;
    private SameCityAddressHistoryAdapter mAdapter;
    private int type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        type = getIntent().getIntExtra("type", 0);
        super.onCreate(savedInstanceState);
        showLoadingDialog(null);
        AppHttp.getInstance().getAddressHistorySameCity(1);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.visit_history_record);
        super.findViewById();
        getAppTitle().setCommonTitle("历史记录");
        mListView = (ListView) findViewById(R.id.listview);
        mAdapter = new SameCityAddressHistoryAdapter(null, getContext());
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SameCityHistoryBean item = (SameCityHistoryBean) parent.getItemAtPosition(position);
                if (null == item) {
                    return;
                }
                if (type == 1) {
                    Intent intent = new Intent();
                    intent.putExtra("item", item);
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                } else {
                    SameCityParcelBean bean = AppCache.getSameCityParcelBean();
                    if (bean == null) {
                        bean = new SameCityParcelBean();
                        AppCache.setSameCityParcelBean(bean);
                    }
                    bean.setSendBuildName(item.getBuildName());
                    bean.setSendAddress(item.getAddress());
                    bean.setSendName(item.getUserName());
                    bean.setSendPhone(item.getPhone());
                    bean.setSendLatitude(item.getLatitude());
                    bean.setSendLongitude(item.getLongitude());
                    bean.setSendProvince(item.getProvince());
                    bean.setSendCity(item.getCity());
                    bean.setSendDistrict(item.getDistrict());
                    bean.setSendAddressDesc(item.getAddressDesc());
                    AppCache.setSameCityParcelBean(bean);
                    if (isEmpty(item.getAddressDesc())) {
                        Intent intent = new Intent(getContext(), SameCityInfoConfirmActivity.class);
                        intent.putExtra("type", 0);
                        intent.putExtra("loadType", 0);
                        intent.putExtra("isFrom", true);
                        startActivityForResult(intent, 1601);
                    } else {
                        setResult(Activity.RESULT_OK);
                        finish();
                    }
                }
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.get_address_history_same_city:
                cancelLoadingDialog();
                final AppResult<SameCityHistoryBean> result = JSON.parseList(msg.obj, SameCityHistoryBean.class);
                if (result.isSuccess()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.setItems(result.getResultList(), true);
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && 1601 == requestCode) {
            setResult(Activity.RESULT_OK);
            finish();
        }
    }
}

